package br.com.diego.system.view.action.permissao;

import br.com.diego.system.controller.cadastropermissao.PermissaoVo;
import br.com.diego.system.controller.consultapermissao.ConsultaPermissaoBuilder;
import br.com.diego.system.controller.consultapermissao.ConsultaPermissaoFactory;
import br.com.diego.system.controller.consultapermissao.ConsultaPermissaoVo;
import br.com.diego.system.view.action.AbstractDiegoAction;
import br.com.diego.system.view.navigator.Navigator;
import br.com.diego.system.view.navigator.permissao.Pages;
import javax.faces.event.ActionEvent;
import org.springframework.beans.factory.annotation.Autowired;

public class ConsultaPermissaoAction extends AbstractDiegoAction {

    @Autowired
    private ConsultaPermissaoFactory consultaPermissaoFactory;
    
    @Autowired
    private ConsultaPermissaoBuilder consultaPermissaoBuilder;    
    
    @Autowired
    private CadastroPermissaoAction cadastroPermissaoAction = new CadastroPermissaoAction();
    
    private ConsultaPermissaoVo consultaPermissaoVo = new  ConsultaPermissaoVo();
    
    private PermissaoVo permissaoVo = new PermissaoVo();
    
    public void consultar(){
        try {
            consultaPermissaoVo.setPermissaoVoList(consultaPermissaoBuilder.consultar(consultaPermissaoVo));
        } catch (IllegalArgumentException e) {
            setMensagem(e.getMessage());
        }
    }
    
    public void limpar() {
        consultaPermissaoVo = new ConsultaPermissaoVo();
    }
    
    public String redirecionar() {
        if (Navigator.getPage().equals(Pages.cadastroPermissao.name())) {
            carregarCadastroPermissao();
            Navigator.setPage("");
            return Pages.cadastroPermissao.name();            
                 
        } else {            
            return Pages.consultaPermissao.name();           
        }
    }   
    
    public void retornar(ActionEvent event) {
        permissaoVo = (PermissaoVo) event.getComponent().getAttributes().get("permissao");        
        permissaoVo = consultaPermissaoBuilder.retornar(permissaoVo);
    }

    public void carregarCadastroPermissao(){
        cadastroPermissaoAction.getCadastroPermissaoVo().setPermissaoVo(permissaoVo);
    }

    public ConsultaPermissaoVo getConsultaPermissaoVo() {
        return consultaPermissaoVo;
    }

    public void setConsultaPermissaoVo(ConsultaPermissaoVo consultaPermissaoVo) {
        this.consultaPermissaoVo = consultaPermissaoVo;
    }

    public PermissaoVo getPermissaoVo() {
        return permissaoVo;
    }

    public void setPermissaoVo(PermissaoVo permissaoVo) {
        this.permissaoVo = permissaoVo;
    }
    
}
